;
(function ($) {
    //获取用户id
    let uid = localStorage.getItem('uid');
    if (uid) {
        $(".fr li:eq(0) a:eq(0)").text("欢迎用户" + uid);
    }
    //获取商品
    let url = `http://jx.xuzhixiang.top/ap/api/cart-list.php`;
    let id = uid;
    // console.log(id);
    axios.get(url, {
        params: {
            id
        }
    }).then(res => {
        let str = "";
        res.data.data.forEach(item => {
            str += `
            <div class="item_list" data-id="${item.pid}">
                    <div class="cell p_checkbox">
                        <input type="checkbox" class="danxuan">
                    </div>
                    <div class="cell p_goods">
                        <div class="goods_con">
                            <div class="goods_con_img">
                                <img src="${item.pimg}" alt="">
                            </div>
                            <div class="goods_con_name">
                                <i class="jingxuan"></i>
                                <i class="chaoshi"></i>
                                ${item.pname}
                            </div>
                        </div>
                    </div>
                    <div class="cell p_props">
                        <div class="p_props_con">
                            ${item.pdesc}
                        </div>
                    </div>
                    <div class="cell p_price">
                        <span class="p_price_con">${item.pprice}</span>
                    </div>
                    <div class="cell p_qut">
                        <div class="p_qut_con">
                            <button class="qut_minus">-</button>
                            <div class="qut_con_num">
                                <input type="text" class="qut_num" value="${item.pnum}">
                            </div>
                            <button class="qut_plus">+</button>
                        </div>
                    </div>
                    <div class="cell p_sum">
                        <span>${item.pnum * item.pprice}</span>
                    </div>
                    <div class="cell p_del">
                        <a href="#none" class="cart_del" data-id="${item.pid}">删除</a>
                    </div>
                </div>
            `
        })
        $(".shop_item").append(str);

        let checkAll = $("#quanxuan")[0]; //全选框
        let totalPrice = $(".cart_zong span")[0]; //总价
        let list = $(".item_list"); //每一个商品外框
        let ck = $(".danxuan"); //每一个单选框
        let minus = $(".qut_minus"); //每一个减号
        let num = $(".qut_num"); //每一个商品数量
        let plus = $(".qut_plus"); //每一个加号
        let perPrice = $(".p_price_con"); //每一个商品单价
        let perTotalPrice = $(".p_sum span"); //每一个商品总价
        let delBtn = $(".cart_del"); //删除按钮

        //全选框操作
        checkAll.onclick = function () {
            for (let i = 0; i < ck.length; i++) {
                ck[i].checked = checkAll.checked;
                getTotalPrice();
                updataData(i);
            }
        }
        //单选框操作
        for (let i = 0; i < ck.length; i++) {
            ck[i].onclick = function () {
                let count = 0;
                for (let j = 0; j < ck.length; j++) {
                    if (ck[j].checked) {
                        count++
                    }
                }
                if (count === ck.length) {
                    checkAll.checked = true;
                } else {
                    checkAll.checked = false;
                }
                getTotalPrice();
                updataData(i);
            }
        }
        //减
        for (let i = 0; i < minus.length; i++) {
            minus[i].onclick = function () {
                num[i].value--;
                if (num[i].value <= 0) {
                    num[i].value = 1;
                }
                updataData(i);
            }
        }
        //加
        for (let i = 0; i < plus.length; i++) {
            plus[i].onclick = function () {
                num[i].value++;
                updataData(i);
            }
        }
        //文本框
        for (let i = 0; i < num.length; i++) {
            num[i].onchange = function () {
                if (num[i].value <= 0) {
                    num[i].value = 1;
                }
                updataData(i);
            }
        }
        //删除
        delData();

        //计算总价
        function getTotalPrice() {
            let totalprice = 0;
            let ck = document.getElementsByClassName("danxuan");
            for (let i = 0; i < ck.length; i++) {
                if (ck[i].checked) {
                    totalprice += +perTotalPrice[i].innerText;
                }
            }
            totalPrice.innerText = totalprice;
            if (ck.length == 0) {
                checkAll.checked = false;
                location.reload();
            }
        }
        //更新数据
        function updataData(i) {
            //单个价格变化
            perTotalPrice[i].innerText = num[i].value * perPrice[i].innerText;
            let pnum = num[i].value;
            let pid = list[i].getAttribute("data-id");
            let url = `http://jx.xuzhixiang.top/ap/api/cart-update-num.php`;
            axios.get(url, {
                params: {
                    uid,
                    pid,
                    pnum
                }
            }).then(res => {
                // console.log(res);
            })
             //总价计算
            getTotalPrice();
        }
        //删除
        function delData() {
            $(".cart_del").click(function () {
                $(this).parent().parent().remove();
                let uid = localStorage.getItem('uid');
                let pid = $(this).attr("data-id");
                let url = `http://jx.xuzhixiang.top/ap/api/cart-delete.php`;
                axios.get(url, {
                    params: {
                        uid,
                        pid
                    }
                }).then(res => {
                    console.log(res);
                })
                getTotalPrice();
            })
        }



    })
})(jQuery)